/*TIP MESSAGE*/ 
/*@date 2012-02-20*/
/*@author HUWENCE*/
/*@version 1.0.0*/

var SQDialog = (function(){
   
   /**
   <div class = 'tipDialog'>
     <div class = 'title'></div>
     <div class = 'content'>
	     <div id = 'tip_icon'>
		 </div>
		 <div></div>
	 </div>
	 <div class = 'advisory'>
	     <div id = 'sure'></div>
		 <div id = 'cancel'></div>
	 </div>
   </div>
   **/

   /*get element by id*/
   function $(id){
      try{
	     return document.getElementById(id); 
	  }catch(e){
	  }
   }
   
   //define
   var dialog = document.createElement('div'), //dialog box
       cover  = document.createElement('div'), //cover
       html   = ["<div class='dialog-title' id='dialogTitle'></div>", //title
                 "<div class='dialog-content'><div class='dialog-icon'></div><div class='dialog-text' id='dialogText'></div></div>", //content
                 "<div class='dialog-advisory'><div class='dialog-button' id='dialogSure'>确定</div><div class='dialog-button' id='dialogCancel'>取消</div></div>"  //advisory
	            ];

   //init
   dialog.className = 'dialog-box';
   dialog.innerHTML = html.join('');
   cover.className  = 'dialog-bgcover';
   cover.style.height = document.documentElement.offsetHeight;

   //load
   document.body.appendChild(dialog);
   document.body.appendChild(cover);

   var elem = {
      'title'  : $('dialogTitle'),
	  'text'   : $('dialogText'),
	  'sure'   : $('dialogSure'),
	  'cancel' : $('dialogCancel')
   }

   function hide(){
      dialog.style.visibility = 'hidden';
	  cover.style.display  = 'none';
   }
 
   function show(){
	  resizeChange();
      dialog.style.visibility = 'visible';
	  cover.style.display  = 'block';
   }

   function resizeChange(){
	   var height = dialog.offsetHeight,
		   width  = 90 + elem.text.offsetWidth,
		   clientHeight = document.documentElement.clientHeight; 
	   dialog.style.marginTop  = '-' + (height / 2) + 'px';
	   dialog.style.marginLeft = '-' + (width / 2) + 'px';
	   dialog.style.width   =  width + 'px';
	   cover.style.height   = clientHeight + 'px';
   }

   function coversize(){
       var height = document.documentElement.clientHeight;
	   cover.style.height = height + 'px';
   }

   function _alert(content, title){
      elem.title.innerHTML = title != undefined ? title : '来自社圈网的消息';
      elem.text.innerHTML  = content != undefined ? content : '';
	  elem.cancel.style.display = 'none';
	  show();

	  elem.sure.onclick = hide;
   }

   function _confirm(content, title){
	  var result = false;
      elem.title.innerHTML = title != undefined ? title : '';
      elem.text.innerHTML  = content != undefined ? content : '';
	  elem.cancel.style.display = 'block';
	  show();
	  elem.sure.onclick = function(){
	      hide();
		  result = true;
	  }

	  elem.cancel.onclick = hide;
	  return result;
   }

   var mouseEffect = {
      'onmouseout': function(){
	      this.style.backgroundPosition = '0 0';  
	  },

	  'onmouseover': function(){
	      this.style.backgroundPosition = '0 -25px';  
	  }
   }

   window.onresize = coversize;
   var action = ['onmouseout', 'onmouseover']
   for (var i in action){
	   var attr = action[i];
       elem.sure[attr] = mouseEffect[attr]; 
	   elem.cancel[attr] = mouseEffect[attr];
   }

   return {
      Alert: _alert, 
	  Confirm: _confirm
   }
})();
